package com.cac.demo.controller.logMonitor;

import com.cac.demo.dao.logMonitor.ErpLogDao;
import com.cac.demo.service.MsSslShelves.MsSslShelvesService;
import com.cac.demo.util.CreateCommonLog;
import com.cac.demo.util.adapter.ResponseMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Objects;

/**
 * @className: TaskGoOnResy
 * @description: （描述这个类的作用）
 * @author:
 * @date: 2023/6/12 12:30:30 周一
 * @version: 1.0
 */

@RestController
@RequestMapping("/taskGoOn")
@Slf4j
public class TaskGoOnRest {

    @Autowired
    private CreateCommonLog createCommonLog;
    @Autowired
    private MsSslShelvesService msSslShelvesService;
    @Autowired
    private ErpLogDao erpLogDao;
    /**
     * 物料接收 继续执行任务按钮
     * */
    @PostMapping("/receiveGoingOn")
    public ResponseMsg taskGoOn(){
        ResponseMsg responseMsg =new ResponseMsg();
        String IP = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
        String operationBit = null;
        String boxType = "WORKBIN";
        HashMap outFirst = new HashMap();
        //获取操作编码
        operationBit = msSslShelvesService.findStartCode(IP, boxType);
        outFirst.put("endCode", "xyssj" + operationBit);
        outFirst.put("func", "receive");
        //调用流量控制
        HashMap callRtrun = createCommonLog.callWCSInterface(outFirst);
        responseMsg.setRetCode((String)callRtrun.get("retCode")).setMessage((String)callRtrun.get("msg"));
        return responseMsg;
    }

    /**
     * 分拣操作 继续执行任务按钮
     * */
    @PostMapping("/sortGoingOn")
    public ResponseMsg TaskGoingOn(){
        ResponseMsg responseMsg =new ResponseMsg();

        String IP = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
        String operationBit = null;
        HashMap workbin = new HashMap();
        //获取操作编码
        operationBit = msSslShelvesService.findStartCode(IP, "WORKBIN");
        workbin.put("endCode", "xyssj" + operationBit);
        workbin.put("func", "sort");
        workbin.put("boxType", "WORKBIN");
        //调用流量控制->缓存箱
        HashMap callRtrun = createCommonLog.callWCSInterface(workbin);
        if("201".equals(callRtrun.get("retCode").toString())){
            responseMsg.setRetCode((String)callRtrun.get("retCode")).setMessage((String)callRtrun.get("msg"));
            return responseMsg;
        }
        //调用流量控制->托盘
        HashMap tool =new HashMap();
        operationBit = msSslShelvesService.findStartCode(IP, "TOOL");
        tool.put("endCode", "xyssj" + operationBit);
        tool.put("func", "sort");
        tool.put("boxType", "TOOL");
        HashMap rsl= createCommonLog.callWCSInterface(tool);
        if("201".equals(rsl.get("retCode").toString())){
            responseMsg.setRetCode((String)rsl.get("retCode")).setMessage((String)rsl.get("msg"));
            return responseMsg;
        }
        responseMsg.setRetCode((String)callRtrun.get("retCode")).setMessage((String)callRtrun.get("msg"));
        return responseMsg;
    }


    //日志查询，继续下发
    @PostMapping("/LoggingGoingOn")
    public ResponseMsg LoggingtaskGoOn(@RequestBody HashMap<String, String> map){
        ResponseMsg responseMsg =new ResponseMsg();
        String logId = map.get("msGkLogId");
        String IP = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
        String operationBit = null;
        String boxType = "WORKBIN";
        HashMap outFirst = new HashMap();
        //获取操作编码
//        operationBit = msSslShelvesService.findStartCode(IP, boxType);
//        outFirst.put("endCode", "xyssj" + operationBit);
//        outFirst.put("func", "receive");
        //调用流量控制
//        HashMap callRtrun = createCommonLog.callWCSInterface(outFirst);
//        responseMsg.setRetCode((String)callRtrun.get("retCode")).setMessage((String)callRtrun.get("msg"));

        HashMap rsl=erpLogDao.searchLogByLogId(logId);
        if(Objects.isNull(rsl.get("orderTime")) && Objects.isNull(rsl.get("parentTaskId"))&& "N".equals(rsl.get("isFinish")) ){
            String res=msSslShelvesService.doTask(rsl);
            responseMsg.setRetCode("200").setMessage("执行完毕");
        }else{
            responseMsg.setRetCode("201").setMessage("该任务已经下发wcs，不允许重复下发");
        }
        return responseMsg;
    }
}
